// 入口文件
import Vue from 'vue';
//mint-ui
// import { Header,Search ,Swipe, SwipeItem,Button,Actionsheet,Field,Lazyload } from 'mint-ui';
// Vue.component(Header.name, Header);
// Vue.component(Swipe.name, Swipe);
// Vue.component(SwipeItem.name, SwipeItem);
// Vue.component(Search.name, Search);
// Vue.component(Button.name, Button);
// Vue.component(Actionsheet.name, Actionsheet);
// Vue.component(Field.name, Field);
// Vue.use(Lazyload);
import MinUI from 'mint-ui';

Vue.use(MinUI);
import 'mint-ui/lib/style.css'
//vue相关
import VueRouter from 'vue-router';

Vue.use(VueRouter);
import VueResource from 'vue-resource';

Vue.use(VueResource);
import VuePreview from 'vue-preview';

Vue.use(VuePreview);
import Vuex from 'vuex';

Vue.use(Vuex);
//设置路由的跟路径
Vue.http.options.root = 'https://locally.uieee.com';
//引入的文件mui
import './lib/mui/css/mui.min.css';
import './fonts/mui-icons-extra.ttf';
import './lib/mui/css/icons-extra.css';
import app from './App.vue';
import router from './router.js';

//vuex

var car = JSON.parse(localStorage.getItem('car') || '[]');

var store = new Vuex.Store({
    state: {
        car: car
    },
    mutations: {
        addToCar(state, goodsinfo) {
            var flag = false;
            state.car.some(item => {
                if (item.id == goodsinfo.id) {
                    item.count += parseInt(goodsinfo.count);
                    flag = true;
                    return true
                }
            });
            if (!flag) {
                state.car.push(goodsinfo)
            }
            localStorage.setItem('car', JSON.stringify(state.car))
        },
        updateCar(state, goodsinfo) {
            state.car.some(item => {
                if (item.id == goodsinfo.id) {                  //将得到的count相加，然后将数据放入缓存中
                    item.count = parseInt(goodsinfo.count);
                    return true
                }
            });
            localStorage.setItem('car', JSON.stringify(state.car))
        },
        removeGoods(state, id) {
            state.car.some((item, i) => {
                if (item.id == id) {
                    state.car.splice(i, 1);
                    return true
                }
            });
            localStorage.setItem('car', JSON.stringify(state.car))
        }
    },
    getters: {
        sum(state) {
            var s = 0;
            state.car.forEach(item => {
                s += item.count
            });
            return s;
        },
        getGoodsCount(state) {
            var o = {};
            state.car.forEach(item => {      //需要遍历购物车中的每一个得到count
                o[item.id] = item.count
            });
            return o
        },
        getSelected(state){
            var x = {};
            state.car.forEach(item => {
                x[item.id] = item.selected
            });
            return x
        }
    }
});

var v = new Vue({
    el: "#dv",
    render: c => c(app),
    router,
    store,
})